import type { Schema } from '../../../schema/index.js'

export const mergeSchemas = (target: Schema, source: Schema) => {
  for (const [tableName, table] of Object.entries(source.tables)) {
    target.tables[tableName] = {
      ...target.tables[tableName],
      ...table,
      columns: {
        ...target.tables[tableName]?.columns,
        ...table.columns,
      },
      indexes: {
        ...target.tables[tableName]?.indexes,
        ...table.indexes,
      },
      constraints: {
        ...target.tables[tableName]?.constraints,
        ...table.constraints,
      },
    }
  }

  // Merge enums
  for (const [enumName, enumObj] of Object.entries(source.enums)) {
    target.enums[enumName] = {
      ...target.enums[enumName],
      ...enumObj,
    }
  }

  // Merge extensions
  for (const [extensionName, extensionObj] of Object.entries(
    source.extensions,
  )) {
    target.extensions[extensionName] = {
      ...target.extensions[extensionName],
      ...extensionObj,
    }
  }
}
